package com.appkubes.mall.admin.application.order.order.service;

import com.alibaba.fastjson.JSONObject;
import com.appkubes.common.constants.BaseConstants;
import com.appkubes.common.utils.data.date.DateUtils;
import com.appkubes.mall.admin.application.order.order.dao.OrderDao;
import com.appkubes.mall.admin.application.order.order.entity.model.GetListOrderResult;
import com.appkubes.mall.admin.application.order.order.entity.model.OrderSum;
import com.appkubes.mall.admin.application.order.order.entity.model.SaleDataForWeek;
import com.appkubes.mall.admin.application.order.order.entity.model.UserConsume;
import com.appkubes.mall.admin.application.plugin.rent.service.PluginRentService;
import com.appkubes.mall.core.po.MallOrder;
import com.appkubes.mall.core.po.MallOrderExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class OrderServiceDepreciate {
    @Autowired
    OrderDao orderDao;
    @Resource
    PluginRentService pluginRentService;

    public long getCountOfSaleDate(Integer createTime) {
        MallOrderExample example = new MallOrderExample();
        MallOrderExample.Criteria criteria = example.createCriteria();
        criteria.andCreateTimeGreaterThan(createTime);
        List<String> orderState = new ArrayList<>();
        orderState.add(BaseConstants.ORDER.UNPAY);
        orderState.add(BaseConstants.ORDER.CANCEL);
        orderState.add(BaseConstants.ORDER.PAYFAIL);
        criteria.andStatusNotIn(orderState);
        return orderDao.countByExample(example);
    }

    public double sumPayAmount(Integer createTime) {
        UserConsume userConsume = orderDao.sumPayAmount(createTime);
        if (userConsume == null) return (double) 0;
        return userConsume.getTotalAmount();
    }


    public PageInfo querySelective(String name, String appKey, Integer page, Integer size, String sort, String order) {
        MallOrderExample example = new MallOrderExample();
        MallOrderExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtils.isEmpty(appKey)) {
            criteria.andAppKeyEqualTo(appKey);
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, size);
        List<MallOrder> list = orderDao.selectByExample(example);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }



    public PageInfo getList(String queryStr, int page, int limit) {
        List<GetListOrderResult> list = orderDao.getList(queryStr, (page - 1) * limit, limit, false);
        PageInfo pageInfo = new PageInfo(list);
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(limit);
        int count = 0;
        List<GetListOrderResult> total_list = orderDao.getList(queryStr, 0, 0, true);
        if (total_list != null) count = total_list.get(0).getTotal();
        pageInfo.setTotal(count);
        return pageInfo;
    }


    public MallOrder findById(String orderKey) {
        return orderDao.selectByPrimaryKey(orderKey);
    }


    public int add(MallOrder model) {
        model.setCreateTime(DateUtils.DateToTimestamp(new Date()));
        model.setUpdateTime(DateUtils.DateToTimestamp(new Date()));
        model.setAppKey(null);
        return orderDao.insertSelective(model);
    }


    public int update(MallOrder model) {
        model.setUpdateTime(DateUtils.DateToTimestamp(new Date()));
        return orderDao.updateByPrimaryKeySelective(model);
    }

    public int deleteById(String orderKey) {
        return orderDao.deleteByPrimaryKey(orderKey);
    }

    public int deleteByIds(String[] ids) {
        return orderDao.delete(ids);
    }

    public List<String> getPayUserIds(String appKey) {
        List<String> newArr = new ArrayList<>();
        List<String> list = orderDao.getPayUserIds(appKey);
        for (String node : list) {
            if (!StringUtils.isEmpty(node)) {
                newArr.add(node);
            }
        }
        return newArr;
    }

    public List<UserConsume> getUserConsumeRank(String appKey) {
        List<UserConsume> consumes = orderDao.getUserConsumeRank(appKey);
        return consumes;
    }

    public List<SaleDataForWeek> getSaleDataForWeek(String appKey) {
        List<SaleDataForWeek> saleDataForWeek = orderDao.getSaleDataForWeek(appKey);
        return saleDataForWeek;
    }

    public List<OrderSum> getOrderSum(String appKey) {
        List<OrderSum> orderSums = orderDao.getOrderSum(appKey);
        return orderSums;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    public void sendDelivery(String orderKey, String sendKdCode, String sendOrderno) throws RuntimeException {
        MallOrder order = findById(orderKey);
        order.setStatus(BaseConstants.ORDER.SENDING);
        order.setSendKdCode(sendKdCode);
        order.setSendOrderno(sendOrderno);
        update(order);

        JSONObject jsonObject = pluginRentService.getOrderState(orderKey);
        if (jsonObject.getInteger("isSure") != 1) {
            throw new RuntimeException("租赁单未确认");
        }
    }
}
